﻿using GalaSoft.MvvmLight;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using ASoft.Service;

namespace ASoft.WPF.ViewModel
{
    public partial class PLCDataViewModel : ViewModelBase
    {
        public PLCDataViewModel()
        {
            if (IsInDesignMode)
            {
                this._PortName = "COM1";
                this.PortNameSource = new string[] { "COM1", "COM2" };
            }
            else
            {
                this.PortNameSource = SerialPortService.GetPortNames();
                WPFUtility.TransFromDataToViewModel(RC.Current.RootData.PLCData, this);
            }
        }

        ////public override void Cleanup()
        ////{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}

        public string[] PortNameSource { get; private set; }



        public ICommand SaveCommand
        {
            get
            {
                return new RelayCommand(
                    () =>
                    {
                        WPFUtility.TransFromViewModelToData(this, RC.Current.RootData.PLCData);
                        RC.Current.SaveRootData();
                        RC.Current.ReLoadRootData();
                        RaisePropertyChanged("PortText");
                    },
                    () =>
                    {
                        if (IsInDesignMode)
                        {
                            return false;
                        }
                        //return !WPFUtility.DataEqualsViewModel(RC.Current.RootData.PLCData, this);
                        return true;
                    });
            }
        }
    }
}